/*----------------------------------------------------------
* FILE: ServiceFinderImpl.java
* LOGICAL NAME: ServiceFinderImpl
* PRODUCT: bila-framework
*----------------------------------------------------------
* MODIFICATIONS
* DATE Oct 8, 2011
* AUTHOR wjuarez
*----------------------------------------------------------
*/

package bila.framework.services.impl;

import java.io.Serializable;
import java.util.List;

import bila.framework.commons.BeanHelper;
import bila.framework.commons.GeneralFunction;
import bila.framework.dao.Dao;
import bila.framework.services.ModelDtoConvertor;
import bila.framework.services.ServiceFinder;

/**
 * @author WJuarez
 * <b>Created by </b>wjuarez
 * <b>for</b> bila-framework
 * <b>package</b> bila.framework.services.impl
 * <b>on</b> Oct 8, 2011 1:08:32 PM
 * <b>Purpose</b> 
 * <p>
 *		Implementacion de ServiceFinderImpl
 * </p>
 */
public abstract class ServiceFinderImpl<Dto, Model, ModelId extends Serializable> extends ModelDtoConvertor<Dto, Model, ModelId> implements ServiceFinder<Dto, Model, ModelId> {

	public ServiceFinderImpl(Class<Dto> dtoClass, Class<Model> modelClass,
			Class<ModelId> modelIdClass) {
		super(dtoClass, modelClass, modelIdClass);
	}

	/*
	 * (non-Javadoc)
	 * @see bila.framework.services.ServiceFinder#find(java.lang.Object)
	 */
	public List<Dto> find(Dto dto) throws Exception{
		Model model = createModel(dto);
		List<Model> modelList = getDao().find(model);
		List<Dto> dtoList = createDtoList(modelList);
		return dtoList;
	}

	/*
	 * (non-Javadoc)
	 * @see bila.framework.services.ServiceFinder#findByKey(java.lang.Object)
	 */
	public Dto findByKey(Dto dto) throws Exception{
		ModelId id = createId(dto);
		Model model = getDao().findByKey(id);
		Dto dtoNew = createDto(model);
		return dtoNew;
	}

	/*
	 * (non-Javadoc)
	 * @see bila.framework.services.ServiceFinder#getAll()
	 */
	public List<Dto> getAll() throws Exception{
		List<Model> modelList = getDao().getAll();
		List<Dto> dtoList = createDtoList(modelList);
		return dtoList;
	}

	/*
	 * (non-Javadoc)
	 * @see bila.framework.services.ServiceFinder#getByExample(java.lang.Object)
	 */
	public List<Dto> getByExample(Dto dto) throws Exception{
		Model model = createModel(dto);
		List<Model> modelList = getDao().getByExample(model);
		List<Dto> dtoList = createDtoList(modelList);
		return dtoList;
	}

	/*
	 * (non-Javadoc)
	 * @see bila.framework.services.ServiceFinder#findWithOrderBy(java.lang.Object, java.lang.String[], java.lang.String[])
	 */
	public List<Dto> findWithOrderBy(Dto dto, String[] orders, String[] orderType) throws Exception{
		Model model = createModel(dto);
		List<Model> modelList = getDao().findWithOrderBy(model, orders, orderType);
		List<Dto> dtoList = createDtoList(modelList);
		return dtoList;
	}

	/*
	 * (non-Javadoc)
	 * @see bila.framework.services.ServiceFinder#getAllWithOrderBy(java.lang.String[], java.lang.String[])
	 */
	public List<Dto> getAllWithOrderBy(String[] orders, String[] orderType) throws Exception{
		List<Model> modelList = getDao().getAllWithOrderBy(orders, orderType);
		List<Dto> dtoList = createDtoList(modelList);
		return dtoList;
	}

	public abstract Dao<Model, ModelId> getDao();

	public abstract void setDao(Dao<Model, ModelId> dao);

	/*
	 * (non-Javadoc)
	 * @see bila.framework.services.ModelDtoConvertor#createModel(java.lang.Object)
	 */
	@Override
	public Model createModel(Dto dto) throws Exception{
		Model model = getModelClass().newInstance();
		if(!GeneralFunction.isNullOrEmpty(dto))
			BeanHelper.copyAttributes(dto, model, null);
		return model;
	}
	
	/*
	 * (non-Javadoc)
	 * @see bila.framework.services.ModelDtoConvertor#createDto(java.lang.Object)
	 */
	@Override
	public Dto createDto(Model model) throws Exception{
		Dto dto = getDtoClass().newInstance();
		if(!GeneralFunction.isNullOrEmpty(model))
			BeanHelper.copyAttributes(model, dto, null);
		return dto;
	}
}
